package com.tools.cleanmaster.ui.notification.item;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.text.Html;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.cpucool.CpuCoolManager;
import com.tools.cleanmaster.cpucool.CpuTempInfo;
import com.tools.cleanmaster.scenenew.SceneConstants;
import com.tools.cleanmaster.scenenew.SceneType;
import com.tools.cleanmaster.store.CpuCoolSharedPrefManager;
import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.ui.activity.CpuCoolActivity;
import com.tools.cleanmaster.ui.notification.NotificationBaseItem;
import com.tools.cleanmaster.ui.notification.UIBuilder;
import com.tools.cleanmaster.ui.notification.UIType;
import com.tools.cleanmaster.utils.FeatureConfig;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * CPU降温
 */
public class CpuCoolerItem extends NotificationBaseItem {

    private static final String JSON_KEY_SWITCH = "switch";
    private static final String JSON_KEY_INTERVAL = "interval";
    public static final String JSON_KEY_TEM = "tem";
    private static final boolean DEFAULT_SWITCH = true;
    private static final int DEFAULT_INTERVAL_HOUR = 8;

    private boolean mSwitch = DEFAULT_SWITCH;     //开关，默认开
    private int mIntervalHour = DEFAULT_INTERVAL_HOUR;    //时间间隔，单位h,默认8小时
    private int mMinTem = CpuCoolSharedPrefManager.DEFAULT_MIN_TEM; //cpu温度, 取降温中的默认值

    private CpuTempInfo info;

    public CpuCoolerItem() {
        String jsonBody = SceneSharedPrefManager.getSingleSceneConfigData(getSceneType());
        try {
            JSONObject jsonObject = new JSONObject(jsonBody);
            mSwitch = jsonObject.optBoolean(JSON_KEY_SWITCH, DEFAULT_SWITCH);
            mIntervalHour = jsonObject.optInt(JSON_KEY_INTERVAL, DEFAULT_INTERVAL_HOUR);
            mMinTem = jsonObject.optInt(JSON_KEY_TEM, CpuCoolSharedPrefManager.DEFAULT_MIN_TEM);
        } catch (JSONException e) {
            if (FeatureConfig.DEBUG_LOG) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean isSceneValid() {
        if (!mSwitch) {
            if (FeatureConfig.DEBUG_LOG) {
                log("开关关闭，无法展示");
            }
            return false;
        }

        info = CpuCoolManager.getInstance().startGetCpuTempSync(false, true);
        int thresholdTem = mMinTem;
        boolean isValid = info.getCpuTemp() > thresholdTem;
        if (FeatureConfig.DEBUG_LOG) {
            log("cpu温度:" + info.getCpuTemp() + ",配置温度:" + thresholdTem + ",能否展示：" + isValid);
        }
        return isValid;
    }

    @Override
    public Notification prepareNotification() {
        Context ctx = CleanApplication.getInstance();
        Intent intent = new Intent(ctx, CpuCoolActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        UIParam param = new UIParam();
        param.viewType = UIType.TITLE_WITH_CONTENT_GREEN_BTN;
        param.sceneType = getSceneType();
        String ticketString = ctx.getString(R.string.cpu_cooler_tickertext);
        param.tickerText = Html.fromHtml(ticketString);
        param.title = Html.fromHtml(ctx.getString(R.string.cpu_cooler_title, ticketString));//这个文案根据tickerText走
        param.content = Html.fromHtml(ctx.getString(R.string.cpu_cooler_message));
        param.contentIntent = intent;
        param.btnText = ctx.getString(R.string.cpu_cooler_right_icon_text).toUpperCase();
        param.leftIconResId = R.drawable.ic_scene_cpucool;
        param.tickerIcon = R.drawable.ic_notify_scene_cpucool;
        UIBuilder builder = new UIBuilder(param);
        return builder.build();
    }

    @Override
    public SceneType getSceneType() {
        return SceneType.CPU_COOLER;
    }

    @Override
    public boolean isTimeValid() {
        long lastShow = SceneSharedPrefManager.getLastSceneShowTime(getSceneType());
        long now = System.currentTimeMillis();
        if (now - lastShow < mIntervalHour * SceneConstants.HOUR_MS) {
            if (FeatureConfig.DEBUG_LOG) {
                log("距离上次展示小于 " + mIntervalHour + " 小时，无法展示");
            }
            return false;
        }
        return true;
    }

}
